using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace BF
{
    public static class StringExtension
    {
        public static bool IsNullOrEmpty(this string self)
        {
            return string.IsNullOrEmpty(self);
        }

        public static string UppercaseFirst(this string self)
        {
            if (self.IsNullOrEmpty())
                return self;
            return char.ToUpper(self[0]) + self.Substring(1);
        }

        public static string LowercaseFirst(this string self)
        {
            if (self.IsNullOrEmpty())
                return self;
            return char.ToLower(self[0]) + self.Substring(1);
        }

        public static int ToInt(this string self, int defaultValue = 0)
        {
            return int.TryParse(self, out int retValue) ? retValue : defaultValue;
        }

        public static float ToFloat(this string self, float defaultValue = 0)
        {
            return float.TryParse(self, out float retValue) ? retValue : defaultValue;
        }

        public static string Reverse(this string self)
        {
            char[] chars = self.ToCharArray();
            Array.Reverse(chars);
            return new string(chars);
        }

        /// <summary>
        /// 是否存在中文字符
        /// </summary>
        /// <param name="self"></param>
        /// <returns></returns>
        public static bool HasChinese(this string self)
        {
            return Regex.IsMatch(self, @"[\u4e00-\u9fa5]");
        }
    }
}
